//
//  DuiHuanNewsController.m
//  PosApp
//
//  Created by 刘萍 on 2018/9/10.
//  Copyright © 2018年 李英新. All rights reserved.
//

#import "DuiHuanNewsController.h"
#import "DuiHuanHeadView.h"
#import "DuiHuanNewsCell.h"
#import "JiFenNewsModel.h"
#import "DuiHuanNewsModel.h"

#import "DuiHuanOrderController.h"
#import "KFWXImageController.h"


@interface DuiHuanNewsController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIScrollView *NewsScrollView;
@property (strong, nonatomic) UITableView *NewsTable;
@property (strong, nonatomic) UIImageView * ContentImage;
@property (strong, nonatomic) NSMutableArray * ContentArr;
@property (strong, nonatomic) MBProgressHUD * hud;
@property (strong, nonatomic) DuiHuanNewsModel * DHmodel;

@end

@implementation DuiHuanNewsController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"兑换详情";
    for ( int i = 0; i < self.DataArr.count; i ++) {
        NSLog(@"__%@",self.DataArr[i]);
    }
    
    self.NewsTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreenW, self.DataArr.count*30+38) style:UITableViewStyleGrouped];
    self.NewsTable.delegate = self;
    self.NewsTable.dataSource = self;
    self.NewsTable.rowHeight = 30;
    self.NewsTable.scrollEnabled = NO;
    self.NewsTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.NewsTable registerNib:[UINib nibWithNibName:@"DuiHuanNewsCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [self.NewsScrollView addSubview:self.NewsTable];
    self.NewsScrollView.contentSize = CGSizeMake(0, CGRectGetMaxY(self.NewsTable.frame));
    
    self.ContentImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.NewsTable.frame), KScreenW, KScreenH)];
    [self.NewsScrollView addSubview:self.ContentImage];
    self.NewsScrollView.contentSize = CGSizeMake(0, CGRectGetMaxY(self.ContentImage.frame));
    JiFenNewsModel * model = self.DataArr[0];
    [self.ContentImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",model.bank_exchange]] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        NSLog(@"宽：%f, 高：%f", image.size.width, image.size.height);
        
        self.ContentImage.frame = CGRectMake(0, CGRectGetMaxY(self.NewsTable.frame), KScreenW, KScreenW*image.size.height/image.size.width);
        self.NewsScrollView.contentSize = CGSizeMake(0, CGRectGetMaxY(self.ContentImage.frame));
        
    }];
    
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.DataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DuiHuanNewsCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[DuiHuanNewsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    JiFenNewsModel * model = self.DataArr[indexPath.row];
    [cell setModel:model];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    DuiHuanHeadView * headV = [[NSBundle mainBundle] loadNibNamed:@"DuiHuanHeadView" owner:self options:nil][0];
    
    return headV;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 8;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * footV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenW, 8)];
    footV.backgroundColor = [UIColor whiteColor];
    return footV;
}




- (IBAction)ClickOnBtnToShowKFImage:(id)sender {
    
    KFWXImageController * KFC = [[KFWXImageController alloc] init];
    KFC.modalPresentationStyle = UIModalPresentationOverFullScreen;
    [self presentViewController:KFC animated:YES completion:nil];
}
- (IBAction)ClickOnBtnOfDuiHuanNewsControllerToDuiHuanOrderController:(id)sender {
    DuiHuanOrderController * DHOrder = [[DuiHuanOrderController alloc] init];
    DHOrder.ShowType = self.DHmodel.bank_uppic;
    DHOrder.TypeArr = self.DataArr;
    DHOrder.ChooseID = 0;
    [self.navigationController pushViewController:DHOrder animated:YES];
}

- (NSMutableArray *)ContentArr {
    if (!_ContentArr) {
        _ContentArr = [NSMutableArray array];
    }
    return _ContentArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
